package com.example.project.dao.sqls;

import com.example.project.entity.TableA;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;

/**
 * Created by Ethan on 2022-08-24 22:40
 */
@Slf4j
public class TableAProvider {
    /**
     * 查找A表中所有的值
     * @return
     */
    public String findAllSql() {
        return new SQL()
                .SELECT("*")
                .FROM("a")
                .toString();
    }

    /**
     * 按字段“A”模糊查询
     */
    public String likeSql(String s) {
        return new SQL()
                .SELECT("*")
                .FROM("a")
                .WHERE("A like " + s)
                .toString();
    }

    /**
     * 增加
     */
    public String insertSql() {
        return new SQL()
                .INSERT_INTO("a")
                .VALUES("id,A,B,C,D,E,F,aa,bb,cc,dd,ee",
                        "#{id},#{a},#{b},#{c},#{d},#{e},#{f},#{aa},#{bb},#{cc},#{dd},#{ee}")
                .toString();
    }

    /**
     * 删除
     */
    public String deleteSql() {
        return new SQL()
                .DELETE_FROM("a")
                .WHERE("id=#{id}")
                .toString();
    }

    /**
     * 更新
     */
    public String updateSql(final TableA tableA) {
        return new SQL() {{
            UPDATE("a");
            if (tableA.getA() != null) {
                SET("A=#{a}");
            }
            if (tableA.getB() != null) {
                SET("B=#{b}");
            }
            if (tableA.getC() != null) {
                SET("C=#{c}");
            }
            if (tableA.getD() != null) {
                SET("D=#{d}");
            }
            if (tableA.getE() != null) {
                SET("E=#{e}");
            }
            if (tableA.getF() != null) {
                SET("F=#{f}");
            }
            if (tableA.getAa() != null) {
                SET("aa=#{aa}");
            }
            if (tableA.getBb() != null) {
                SET("bb=#{bb}");
            }
            if (tableA.getCc() != null) {
                SET("cc=#{cc}");
            }
            if (tableA.getDd() != null) {
                SET("dd=#{dd}");
            }
            if (tableA.getEe() != null) {
                SET("ee=#{ee}");
            }
            WHERE("id=#{id}");
        }}.toString();
    }

    public String getSumAaSql() {
        return new SQL()
                .SELECT("sum(aa)")
                .FROM("a")
                .toString();
    }

    public String getSumBbSql() {
        return new SQL()
                .SELECT("sum(bb)")
                .FROM("a")
                .toString();
    }

    public String getSumCcSql() {
        return new SQL()
                .SELECT("sum(cc)")
                .FROM("a")
                .toString();
    }

    public String getSumDdSql() {
        return new SQL()
                .SELECT("sum(dd)")
                .FROM("a")
                .toString();
    }

    public String getSumEeSql() {
        return new SQL()
                .SELECT("sum(ee)")
                .FROM("a")
                .toString();
    }
}
